package cn.lagou.part01

import scala.io.StdIn
import scala.util.Random

/**
 * 人机猜拳
 * 1. 选取对战角色
 * 2. 开始对战，用户出拳，与对手进行比较，提示胜负信息
 * 3. 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
 * 4 . 循环对战，当输入“n”时，终止对战，并显示对战结果
 * 5. 游戏结束后显示得分
 */

//创建用户类User，定义类的属性（name，score）和类的方法（showFist())
class User(var name: String, var score: Int) {
  var draw: Int = 0 //平局数
  var victory: Int = 0 //胜局数
  var defeat: Int = 0 //败局数

  def showFist(): Int = {
    var input = StdIn.readInt()
    while (input != 1 && input != 2 && input != 3) {
      println("输入不合法，请重新输入")
      input = StdIn.readInt()
    }
    return input
  }
}

//创建计算机类Computer，定义类的属性（name，score）和类的方法（showFist())
class Computer(var name: String, var score: Int) {
  var draw: Int = 0 //电脑平局数
  var victory: Int = 0 //电脑胜局数
  var defeat: Int = 0 //电脑败局数

  def showFist(): Int = {
    Random.nextInt(3) + 1
  }
}

//创建游戏类Game，定义类的属性(用户玩家，电脑玩家，对战次数)
class Game {
  var user:User = _          //定义用户玩家
  var computer:Computer = _  //定义电脑玩家
  var battleNum:Int = _      //定义对战次数

  //初始化方法
  def gameInit(): Unit = {
    println("--------------------欢迎进入游戏世界--------------------------")
    println("********************************")
    println("*********猜拳，开始*************")
    println("********************************")
    println()
    println("请选择对战角色：(1.刘备  2.关羽  3.张飞)")
    var num = StdIn.readInt()
    while (num != 1 && num != 2 && num != 3) {
      println("没有这个选项，请重新输入")
      num = StdIn.readInt()
    }
    val battleRole = num match {
      case 1 => "刘备"
      case 2 => "关羽"
      case 3 => "张飞"
    }
    println(s"你选择了与${battleRole}对战")
    user = new User("游客", 0) //初始化玩家信息
    computer = new Computer(s"$battleRole", 0) //初始化玩家信息
    battleNum = 0
  }

  //游戏开始方法
  def startGame(): Unit = {
    var flag = false
    var isStartNext: Char = 'y'
    //循环对战
    while (!flag) {
      battleNum +=1
      //玩家出拳
      println("请出拳！1.剪刀 2.石头 3.布")
      val userFrist = user.showFist()
      userFrist match {
        case 1 => println("你出拳：剪刀")
        case 2 => println("你出拳：石头")
        case 3 => println("你出拳：布")
      }
      //电脑出拳
      println(s"${computer.name}出拳！")
      val computerFirst = computer.showFist()
      computerFirst match {
        case 1 => println(s"${computer.name}出拳：剪刀")
        case 2 => println(s"${computer.name}出拳：石头")
        case 3 => println(s"${computer.name}出拳：布")
      }
      //调用judge方法，判断胜负
      judge(userFrist, computerFirst)
      //根据用户输入判断是否开启下一轮游戏，n退出
      println("是否开始下一轮(y/n)")
      isStartNext = StdIn.readChar()
      while (isStartNext != 'n' && isStartNext != 'N' && isStartNext != 'y' && isStartNext != 'Y') {
        println("你的输入格式不正确，请重新输入")
        isStartNext = StdIn.readChar()
      }
      if (isStartNext == 'n' || isStartNext == 'N') flag = true
    }
    println("退出游戏！")
    echoResult
  }

  //判断猜拳胜负
  def judge(userFirst: Int, computerFirst: Int): Unit = {
    val judgeNum = userFirst match {
      case 1 => if (computerFirst==1) 0 else if (computerFirst==2) -1 else 1
      case 2 => if (computerFirst==1) 1 else if (computerFirst==2) 0 else -1
      case 3 => if (computerFirst==1) -1 else if (computerFirst==2) 1 else 0
    }

    if (judgeNum ==0) {
      println("结果：和局！下次继续努力")
      user.score += 1
      user.draw += 1
      computer.score += 1
      computer.draw += 1
    } else if (judgeNum == 1) {
      println("结果：恭喜,你赢啦！")
      user.score += 2
      user.victory += 1
      computer.defeat += 1
    } else {
      println("结果：你输了,下次继续努力！")
      user.defeat += 1
      computer.victory += 1
    }
  }

  def echoResult(): Unit = {
    println("----------------------------------------------------------")
    println(s"${user.name} VS ${computer.name}")
    println(s"对战次数${battleNum}次")
    println()
    println()
    println("姓名\t等分\t胜局\t和局\t负局")
    println(s"${user.name}\t${user.score}\t\t${user.victory}\t\t${user.draw}\t\t${user.defeat}")
    println(s"${computer.name}\t${computer.score}\t\t${computer.victory}\t\t${computer.draw}\t\t${computer.defeat}")
  }
}
object Game {
  def main(args: Array[String]): Unit = {
    val game = new Game  // 定义一个游戏对象
    game.gameInit()      //游戏初始化
    println("要开始么？y/n")
    var isStart = StdIn.readChar()
    while (isStart != 'n' && isStart != 'N' && isStart != 'y' && isStart != 'Y') {
      println("没有这个选项，请重新输入")
      isStart = StdIn.readChar()
    }
    //开始游戏
    if (isStart == 'Y' || isStart =='y') game.startGame
  }
}
